//
//  PPContentView.m
//  StatusBarEX
//
//  Created by Garenge on 2020/11/29.
//  Copyright © 2020 鹏鹏. All rights reserved.
//

#import "PPContentView.h"

@interface PPContentView()

@property (weak, nonatomic) IBOutlet UILabel *net4GLabel;
@property (weak, nonatomic) IBOutlet UIView *statusBarView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *statusBarConstraint;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *energyIMVWConstraint;

@end

@implementation PPContentView

- (void)setImage:(UIImage *)image {
    _image = image;
    self.imageView.image = image;

    /// 尝试自动获取顶部状态栏颜色(取界面正中, 状态栏以下三个单位的距离点的颜色)
    UIColor *bgColor = [self colorAtPixel:CGPointMake(self.frame.size.width * 0.5, self.statusBarConstraint.constant + 3)];
    [self refreshStatusBarColor:bgColor];

    
    /** 这边默认设置高度为20, 不自动进行缩放了
     *  屏幕高度
     *  CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
     *  CGFloat scale = 1.0;
     *  if (image.size.width > 0) {
     *      scale = screenWidth / image.size.width;
     *  }
     *  [self refreshSattusBarHeightWithScale:scale];
     */
}

/// 取图片上某一点的颜色
- (UIColor *)colorAtPixel:(CGPoint)point {
    if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, self.image.size.width, self.image.size.height), point)) {
        return nil;
    }

    NSInteger pointX = trunc(point.x);
    NSInteger pointY = trunc(point.y);
    CGImageRef cgImage = self.image.CGImage;
    NSUInteger width = self.image.size.width;
    NSUInteger height = self.image.size.height;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    int bytesPerPixel = 4;
    int bytesPerRow = bytesPerPixel * 1;
    NSUInteger bitsPerComponent = 8;
    unsigned char pixelData[4] = { 0, 0, 0, 0 };
    CGContextRef context = CGBitmapContextCreate(pixelData,
                                                 1,
                                                 1,
                                                 bitsPerComponent,
                                                 bytesPerRow,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast |     kCGBitmapByteOrder32Big);
    CGColorSpaceRelease(colorSpace);
    CGContextSetBlendMode(context, kCGBlendModeCopy);

    CGContextTranslateCTM(context, -pointX, pointY-(CGFloat)height);
    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
    CGContextRelease(context);

    CGFloat red   = (CGFloat)pixelData[0] / 255.0f;
    CGFloat green = (CGFloat)pixelData[1] / 255.0f;
    CGFloat blue  = (CGFloat)pixelData[2] / 255.0f;
    CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
    NSLog(@"%f %f %f %f",(CGFloat)pixelData[0],(CGFloat)pixelData[1],(CGFloat)pixelData[2],(CGFloat)pixelData[3]);
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

- (void)refreshNetWith:(BOOL)isWifi value:(UIImage *)image {
    if (isWifi) {
        self.net4GLabel.hidden = YES;
        self.netIMV.hidden = NO;
        self.netIMV.image = image;
    } else {
        self.net4GLabel.hidden = NO;
        self.netIMV.hidden = YES;
        self.netIMV.image = nil;
    }
}

- (void)refreshEnergyIMVWithImage:(UIImage *)image {
    self.energyIMV.image = image;
    // icon.h / icon.w = view.h / view.w
    // view.w = icon.w * view.h / icon.h
    CGFloat width = image.size.width * self.energyIMV.frame.size.height / image.size.height;
    self.energyIMVWConstraint.constant = width;
}

- (void)refreshRightIMVsWithImages:(NSArray<UIImage *> *)images {
    // 从右向左
    if (images.count > 0) {
        self.right1IMV.hidden = NO;
        self.right1IMV.image = images[0];
    } else {
        self.right1IMV.hidden = YES;
        self.right1IMV.image = nil;
    }

    self.right1IMV.hidden = images.count == 0;
    self.right1IMV.image = images.count > 0 ? images[0] : nil;

    self.right2IMV.hidden = images.count <= 1;
    self.right2IMV.image = images.count > 1 ? images[1] : nil;
    self.right3IMV.hidden = images.count <= 2;
    self.right3IMV.image = images.count > 2 ? images[2] : nil;
}

- (void)refreshStatusBarColor:(UIColor *)color {
    self.statusBarView.backgroundColor = color;
}
- (void)refreshStatusBarTextColor:(UIColor *)color {
    self.operatorLabel.textColor = color;
    self.timeLabel.textColor = color;
    self.net4GLabel.textColor = color;
}

- (void)refreshStatusBarHeightWithHeight:(CGFloat)height {
    [self refreshSattusBarHeightWithScale:height / 20.0];
}

- (NSMutableAttributedString *)timeAttribute:(NSString *)timeString fontSize:(CGFloat)fontSize {
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:timeString];
    [attributedStr addAttributes:
     @{NSKernAttributeName: @(1),
       NSFontAttributeName: [UIFont fontWithName:@"PingFangTC-Semibold" size:fontSize]}
                           range:NSMakeRange(0, timeString.length)];

    return attributedStr;
}
- (void)refreshSattusBarHeightWithScale:(CGFloat)scale {
    self.statusBarConstraint.constant = 20 * scale;
    
    /// 计算电池界面的宽度
    if (self.energyIMV.image) {
        CGSize size = self.energyIMV.image.size;
        // s.h / s.w = S.H / S.W
        // size.height / size.width = 20 * scale / S.W
        CGFloat width = 20 * scale * size.width / (size.height > 0 ? size.height : 20);
        self.energyIMVWConstraint.constant = width;
    }
    
    // UIFont *font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * scale];
    // @"PingFangSC-Medium"
    self.operatorLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * scale];



    self.timeLabel.attributedText = [self timeAttribute:self.timeLabel.text fontSize:12 * scale];
    self.net4GLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:12 * scale];
}

+ (instancetype)createViewFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:self options:nil] lastObject];
}

@end
